package tclp.gui.panels;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.ParseException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import tclp.analysis.AnalysisResult;
import tclp.analysis.CombatLogAnalysis;
import tclp.gui.GUIController;

/**
 *
 * @author Absatz Leoš
 */
public class AnalysisPanel extends JPanel {

  private GUIController controller;

  private JLabel timeText;

  private JLabel totalText;

  public AnalysisPanel(GUIController controller) {
    this.controller = controller;

    initComponents();
  }

  private void initComponents() {
    this.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();

    // Label
    c.gridx = 0;
    c.gridy = 0;
    c.insets = new Insets(10, 10, 10, 10);
    c.anchor = GridBagConstraints.NORTHWEST;
    c.weightx = 1;
    c.gridwidth = 3;
    JLabel label = new JLabel("Analysis");
    this.add(label, c);

    c = new GridBagConstraints();
    c.gridx = 0;
    c.gridy = 1;
    c.insets = new Insets(0, 10, 0, 0);
    c.anchor = GridBagConstraints.NORTHWEST;
    JButton selectButton = new JButton("Select combat log");
    this.add(selectButton, c);

    selectButton.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent e) {
        JFileChooser chooser = new JFileChooser();
        chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);

        int returnVal = chooser.showOpenDialog(null);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
          analyseCL(chooser.getSelectedFile().getAbsolutePath());
        }
      }
    });

    c = new GridBagConstraints();
    c.gridx = 0;
    c.gridy = 2;
    c.insets = new Insets(5, 10, 0, 0);
    c.anchor = GridBagConstraints.NORTHWEST;
    JLabel timeLabel = new JLabel("Time elapsed: ");
    this.add(timeLabel, c);

    c = new GridBagConstraints();
    c.gridx = 1;
    c.gridy = 2;
    c.insets = new Insets(5, 10, 0, 0);
    c.anchor = GridBagConstraints.NORTHWEST;
    timeText = new JLabel();
    this.add(timeText, c);

    c = new GridBagConstraints();
    c.gridx = 0;
    c.gridy = 3;
    c.insets = new Insets(5, 10, 0, 0);
    c.anchor = GridBagConstraints.NORTHWEST;
    JLabel totalLabel = new JLabel("Total damage dealt: ");
    this.add(totalLabel, c);

    c = new GridBagConstraints();
    c.gridx = 1;
    c.gridy = 3;
    c.insets = new Insets(5, 10, 0, 0);
    c.anchor = GridBagConstraints.NORTHWEST;
    totalText = new JLabel();
    this.add(totalText, c);

    c = new GridBagConstraints();
    c.fill = GridBagConstraints.BOTH;
    c.gridx = 0;
    c.gridy = 4;
    c.anchor = GridBagConstraints.NORTHWEST;
    c.weightx = 1;
    c.weighty = 1;
    c.gridwidth = 3;
    JPanel fillPanel = new JPanel();
    this.add(fillPanel, c);
  }

  private void analyseCL(String absolutePath) {
    CombatLogAnalysis cla = new CombatLogAnalysis(absolutePath);
    AnalysisResult result = null;
    try {
      result = cla.analyse();
    } catch (Exception ex) {
      System.out.println(ex);
      ex.printStackTrace();
    }
    
    totalText.setText(Long.toString(result.getDamageDone()));
    
    float time = result.getTimeElapsed();
    time = time / 1000;
    timeText.setText(time + " s");
  }
}
